package se.webbzon.boltzmann.editor.component.imagelist;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.SortedSet;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.component.AbstractComponent;
import se.webbzon.boltzmann.editor.list.NullableListModel;
import se.webbzon.oschi01.resourceset.ResourceSet;

public class ImageSetList extends AbstractComponent {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = -5149900906446097686L;

	// A list of all available images in this panel
	private final JList<ImageSetEntry> imageList;
	
	// The input fields for this panel
	private final JTextField indexInput, speedInput;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public ImageSetList( SortedSet<ImageSetEntry> images,
									String imageSetName,
									double imageIndex,
									double imageSpeed) {
		super("Image");
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		JPanel imagePanel = new JPanel();
		imagePanel.setBorder(BorderFactory.createTitledBorder("Image"));
		NullableListModel<ImageSetEntry> imageListModel =
				new NullableListModel<ImageSetEntry>();
		imageList = new JList<ImageSetEntry>(imageListModel);
		//textureList.setPreferredSize(new Dimension(480,480));
		imageList.setCellRenderer(new ImageSetListCell(new Dimension(480,64)));
		JScrollPane scrollPane = new JScrollPane(imageList);
		scrollPane.setPreferredSize(new Dimension(480,480));
		imagePanel.add(scrollPane);
		
		for (ImageSetEntry image : images) {
			imageListModel.addElement(image);
			if (image.getImageSetName().equals(imageSetName))
				imageList.setSelectedIndex(imageListModel.getSize()-1);
		}
		
		JPanel settingsPanel = new JPanel();
		settingsPanel.setBorder(BorderFactory.createTitledBorder("Image Settings"));
		settingsPanel.setLayout(new GridLayout(2,2));
		settingsPanel.add(new JLabel("Image index:"));
		settingsPanel.add(new JLabel("Image speed:"));
		indexInput = new JTextField("" + imageIndex);
		speedInput = new JTextField("" + imageSpeed);
		settingsPanel.add(indexInput);
		settingsPanel.add(speedInput);
		
		add(imagePanel);
		add(settingsPanel);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the selected image set name from this panel. **/
	public String getImageSetName() {
		ImageSetEntry entry = imageList.getSelectedValue();
		if (entry == null)
			return "";
		else
			return entry.getImageSetName();
	}
	
	/** Returns the selected image index from this panel. **/
	public double getImageIndex() {
		return Double.parseDouble(indexInput.getText());
	}
	
	/** Returns the selected image speed from this panel. **/
	public double getImageSpeed() {
		return Double.parseDouble(speedInput.getText());
	}
	
	/** Returns the selected image from this panel. **/
	public ResourceSet<Image> getImageSet() {
		ImageSetEntry entry = imageList.getSelectedValue();
		if (entry == null)
			return null;
		else
			return entry.getImageSet();
	}

}
